/**
 * 
 */
package com.esiag.pdsteam.referencement.dao;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;

import com.esiag.pdsteam.referencement.bean.Adresse;
import com.esiag.pdsteam.referencement.bean.EPileType;
import com.esiag.pdsteam.referencement.bean.Message;
import com.esiag.pdsteam.referencement.bean.MessageSource;
import com.esiag.pdsteam.referencement.bean.Source;
import com.esiag.pdsteam.referencement.bean.TypeSource;
import com.esiag.pdsteam.referencement.interfaces.IAccess;
import com.esiag.pdsteam.referencement.interfaces.IAdministrate;
import com.esiag.pdsteam.referencement.interfaces.IAdresseRef;



/**
 * @author Admin
 *
 */
public class DataBaseAccess extends AbstractDataBaseAccess implements IAdministrate,IAccess,IAdresseRef{

	private static  String SCRIPT_FILE ;
	public DataBaseAccess(){
		super();
	}
	protected String getScript() throws IOException{
		Properties properties = new Properties();
		properties.load( new FileInputStream("config.properties"));
		SCRIPT_FILE = properties.getProperty("pathFileConfig");
		System.out.println(SCRIPT_FILE);
		File file = new File (SCRIPT_FILE);
		//System.out.println(file.getAbsolutePath());
		byte[] data=null;
		FileInputStream reader = new FileInputStream (file);
		data = new byte[(int)file.length()];
		reader.read (data);
		reader.close();
		return new String( data , "Cp1252" );
	}
	
	public TypeSource findTypeSource(int idTypeSource) throws Exception {
		return selectByID(idTypeSource, TypeSource.class);
	}

	public Source findSource(int idSource) throws Exception {
		return selectByID(idSource, Source.class);
	}

	public Message findMessage(int code) throws Exception {
		return selectByID(code,Message.class);
	}

	public Set<TypeSource> getAllTypeSource() throws Exception {
		return selectAll(TypeSource.class);
	}

	public Set<Source> getAllSource() throws Exception {
		return selectAll(Source.class);
	}

	public Set<Message> getAllMessage() throws Exception {
		return selectAll(Message.class);
	}

	public Set<Source> getAllSourcebyType(int idTypeSource) throws Exception {
		return select(Source.class,new String[]{"idTypeSource"},new Object[]{idTypeSource});
	}

	public Set<Message> getAllMessagebySource(int idSource) throws Exception{
		Set<MessageSource> set=select(MessageSource.class,new String[]{"idSource"},new Object[]{idSource});
		
		Set<Message> res=new HashSet<Message>();
		for(MessageSource messageSource:set)
		{
			Message m=selectByID(messageSource.getCode(),Message.class);
			res.add(m);
		}
		return res;
	}

	public int getCriticite(int idSource, int code) throws Exception {
		Set<MessageSource> messageSource=select(MessageSource.class,new String[]{"idSource","codeMessage"},new Object[]{idSource,code});
		if(messageSource.isEmpty()) throw new Exception("Criticité not found");
		return returnCriticite(messageSource);
	}
	/**
	 * @param messageSource
	 * @return
	 */
	private int returnCriticite(Set<MessageSource> messageSource) {
		Object []messageSources=messageSource.toArray();
		return ((MessageSource)messageSources[0]).getCriticite();
	}
	
	

	public void ajouterTypeSource(TypeSource typeSource) throws Exception {
		insert(typeSource);
		
	}

	public void ajouterSource(Source source) throws Exception {
		// TODO Auto-generated method stub
		insert(source);
		
	}

	public void ajouterMessage(Message message) throws Exception {
		// TODO Auto-generated method stub
		insert(message);
	}

	public void supprimerTypeSource(int idTypeSource) throws Exception {
		// TODO Auto-generated method stub
		deleteByID(idTypeSource, TypeSource.class);
	}

	public void supprimerSource(int idSource) throws Exception {
		// TODO Auto-generated method stub
		deleteByID(idSource, Source.class);
	}

	public void supprimerMessage(int code) throws Exception {
		// TODO Auto-generated method stub
		deleteByID(code, Message.class);
	}

	public void modifierTypeSource(TypeSource typeSource) throws Exception {
		// TODO Auto-generated method stub
		update(typeSource);
	}

	public void modifierSource(Source source) throws Exception {
		// TODO Auto-generated method stub
		update(source);
	}

	public void modifierMessage(Message message) throws Exception {
		// TODO Auto-generated method stub
		update(message);
	}
	@Override
	public DataSource getDataSource() {
		// TODO Auto-generated method stub
		return new HSqlDataSource("jdbc:hsqldb:file:" + absPath + "data;");
	}
	public EPileType getCriticiteEnum(int idSource, int code) throws Exception {
		// TODO Auto-generated method stub
		return EPileType.values()[getCriticite(idSource, code)-1];
	}
	public void ajouterAdresse(Adresse adresse) throws Exception {
		// TODO Auto-generated method stub
		insert(adresse);
	}
	public void supprimerAdresse(String adr) throws Exception {
		// TODO Auto-generated method stub
		deleteByID(adr, Adresse.class);
	}
	public void modifierAdresse(Adresse adresse) throws Exception {
		// TODO Auto-generated method stub
		update(adresse);
	}
	public Adresse getAdresse(int idSource) throws Exception {
		Source source=selectByID(idSource, Source.class);
		return selectByID(source.getAdrMac(), Adresse.class);
	}
}
